<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Support for QSettings &#8212; PyQt 5.8.2 Reference Guide</title>
    
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '5.8.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="shortcut icon" href="_static/logo_tn.ico"/>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Integrating Python and QML" href="qml.html" />
    <link rel="prev" title="Support for QVariant" href="pyqt_qvariant.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="qml.html" title="Integrating Python and QML"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="pyqt_qvariant.html" title="Support for QVariant"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">PyQt 5.8.2 Reference Guide</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="support-for-qsettings">
<h1>Support for <a class="reference internal" href="api/qsettings.html#PyQt5.QtCore.QSettings" title="PyQt5.QtCore.QSettings"><code class="xref py py-class docutils literal"><span class="pre">QSettings</span></code></a><a class="headerlink" href="#support-for-qsettings" title="Permalink to this headline">¶</a></h1>
<p>Qt provies the <a class="reference internal" href="api/qsettings.html#PyQt5.QtCore.QSettings" title="PyQt5.QtCore.QSettings"><code class="xref py py-class docutils literal"><span class="pre">QSettings</span></code></a> class as a platform independent
API for the persistent storage and retrieval of application settings.  Settings
are retrieved using the <code class="xref py py-meth docutils literal"><span class="pre">value()</span></code> method.  However
the type of the value returned may not be what is expected.  Some platforms
only ever store string values which means that the type of the original value
is lost.  Therefore a setting with an integer value of <code class="docutils literal"><span class="pre">42</span></code> may be retrieved
(on some platforms) as a string value of <code class="docutils literal"><span class="pre">'42'</span></code>.</p>
<p>As a solution to this problem PyQt5&#8217;s implementation of
<code class="xref py py-meth docutils literal"><span class="pre">value()</span></code> takes an optional third argument called
<code class="docutils literal"><span class="pre">type</span></code>.  This is either a Python type object, e.g. <code class="docutils literal"><span class="pre">int</span></code>, or a string that
is the name of a C++ type, e.g. <code class="docutils literal"><span class="pre">'QStringList'</span></code>.  The value returned will be
an object of the requested type.</p>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">PyQt5.QtCore</span> <span class="k">import</span> <span class="n">QSettings</span><span class="p">,</span> <span class="n">QPoint</span>

<span class="n">settings</span> <span class="o">=</span> <span class="n">QSettings</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="s1">&#39;foo&#39;</span><span class="p">)</span>

<span class="n">settings</span><span class="o">.</span><span class="n">setValue</span><span class="p">(</span><span class="s1">&#39;int_value&#39;</span><span class="p">,</span> <span class="mi">42</span><span class="p">)</span>
<span class="n">settings</span><span class="o">.</span><span class="n">setValue</span><span class="p">(</span><span class="s1">&#39;point_value&#39;</span><span class="p">,</span> <span class="n">QPoint</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">12</span><span class="p">))</span>

<span class="c1"># This will write the setting to the platform specific storage.</span>
<span class="k">del</span> <span class="n">settings</span>

<span class="n">settings</span> <span class="o">=</span> <span class="n">QSettings</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="s1">&#39;foo&#39;</span><span class="p">)</span>

<span class="n">int_value</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">value</span><span class="p">(</span><span class="s1">&#39;int_value&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;int_value: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="nb">repr</span><span class="p">(</span><span class="n">int_value</span><span class="p">))</span>

<span class="n">point_value</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">value</span><span class="p">(</span><span class="s1">&#39;point_value&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">QPoint</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;point_value: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="nb">repr</span><span class="p">(</span><span class="n">point_value</span><span class="p">))</span>
</pre></div>
</div>
<p>When this is executed then the following will be displayed for all platforms:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">int_value</span><span class="p">:</span> <span class="mi">42</span>
<span class="n">point_value</span><span class="p">:</span> <span class="n">PyQt5</span><span class="o">.</span><span class="n">QtCore</span><span class="o">.</span><span class="n">QPoint</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span>
</pre></div>
</div>
<p>If the value of the setting is a container (corresponding to either
<code class="docutils literal"><span class="pre">QVariantList</span></code>, <code class="docutils literal"><span class="pre">QVariantMap</span></code> or <code class="docutils literal"><span class="pre">QVariantHash</span></code>) then the type is applied
to the contents of the container.</p>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">PyQt5.QtCore</span> <span class="k">import</span> <span class="n">QSettings</span>

<span class="n">settings</span> <span class="o">=</span> <span class="n">QSettings</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="s1">&#39;foo&#39;</span><span class="p">)</span>

<span class="n">settings</span><span class="o">.</span><span class="n">setValue</span><span class="p">(</span><span class="s1">&#39;list_value&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="n">settings</span><span class="o">.</span><span class="n">setValue</span><span class="p">(</span><span class="s1">&#39;dict_value&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;one&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;two&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">})</span>

<span class="c1"># This will write the setting to the platform specific storage.</span>
<span class="k">del</span> <span class="n">settings</span>

<span class="n">settings</span> <span class="o">=</span> <span class="n">QSettings</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="s1">&#39;foo&#39;</span><span class="p">)</span>

<span class="n">list_value</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">value</span><span class="p">(</span><span class="s1">&#39;list_value&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;list_value: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="nb">repr</span><span class="p">(</span><span class="n">list_value</span><span class="p">))</span>

<span class="n">dict_value</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">value</span><span class="p">(</span><span class="s1">&#39;dict_value&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;dict_value: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="nb">repr</span><span class="p">(</span><span class="n">dict_value</span><span class="p">))</span>
</pre></div>
</div>
<p>When this is executed then the following will be displayed for all platforms:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">list_value</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="n">dict_value</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;one&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;two&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">}</span>
</pre></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/logo.png" alt="Logo"/>
            </a></p>
  <h4>Previous topic</h4>
  <p class="topless"><a href="pyqt_qvariant.html"
                        title="previous chapter">Support for <code class="docutils literal"><span class="pre">QVariant</span></code></a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="qml.html"
                        title="next chapter">Integrating Python and QML</a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="qml.html" title="Integrating Python and QML"
             >next</a> |</li>
        <li class="right" >
          <a href="pyqt_qvariant.html" title="Support for QVariant"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">PyQt 5.8.2 Reference Guide</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2015 Riverbank Computing Limited.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.3.
    </div>
  </body>
</html>